import collections
class Solution(object):
    def canDistribute(self, nums, quantity):
        c = collections.Counter(nums)
        m = len(quantity)
        last_set = {0}
        for num in c.values():
            now_set = set()
            def dfs(i, last_val, now_count):
                if i == m:
                    now_set.add(last_val)
                else:
                    dfs(i + 1, last_val, now_count)
                    if now_count + quantity[i] <= num:
                        dfs(i + 1, last_val | (1 << i), now_count + quantity[i])
            dfs(0, 0, 0)
            next_set = set()
            for val0 in last_set:
                for val1 in now_set:
                    next_set.add(val0 | val1)
            last_set = next_set
        if 2 ** m - 1 in last_set:
            return True
        else:
            return False



data = Solution()
nums = [1,2,3,3]
quantity = [2]
print(data.canDistribute(nums, quantity))